﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MoneyManagerNET.Presentation.ViewModel
{
    using Model;
using System.Windows.Input;
using MoneyManagerNET.Presentation.Infrastructure;
    using MoneyManagerNET.Presentation.ViewModel.Base;

    public  class StockInvestmentViewModel:ViewModelBase<IStockInvestment>
    {

        IStockAccount _HeldAtAccount;

        public IStockAccount HeldAtAccount
        {
            get { return _HeldAtAccount; }
            set { _HeldAtAccount = value; this.OnChange(()=>HeldAtName); }
        }


        public string HeldAtName { get { return (HeldAtAccount!=null)?HeldAtAccount.Name:"<select>"; } }

        string _Name;
        public string Name { get { return _Name; } set { _Name = value; OnChange("Name"); } }

        int _Count;
        public int Count { get { return _Count; } set { _Count = value; OnChange("Count"); OnChange("Cost"); } }

        decimal _Commission;
        public decimal Commission { get { return _Commission; } set { _Commission = value; OnChange("Commission"); } }

        string _Notes;
        public string Notes { get { return _Notes; } set { _Notes = value; OnChange("Notes"); } }

        DateTime _Date;
        public DateTime Date { get { return _Date; } set { _Date = value; OnChange("Date"); } }


        string _Symbol;
        public string Symbol { get { return _Symbol; } set { _Symbol = value; OnChange("Symbol"); OnChange("Cost"); } }


        decimal _PurchasePrice;
        public decimal PurchasePrice { get { return _PurchasePrice; } set { _PurchasePrice = value; OnChange("PurchasePrice"); OnChange("Cost"); } }


        decimal _CurrentPrice;
        public decimal CurrentPrice { get { return _CurrentPrice; } set { _CurrentPrice = value; OnChange("CurrentPrice"); OnChange("Cost"); } }


        public decimal Cost { get { return CurrentPrice * Count; } }

        public override void LoadValues(IStockInvestment model)
        {
            base.LoadValues(model);

            this.CurrentPrice = model.CurrentPrice;
            this.Date = model.Date;
            this.Count = model.StockCount;
            this.Commission = model.Commission;
            this.HeldAtAccount = model.HeldAt;
            this.Name = model.StockName;
            this.Notes = model.Notes;
            this.PurchasePrice = model.PurchasePrice;
            this.Symbol = model.StockSymbol;
        }

        public override IStockInvestment PutValues(IStockInvestment model)
        {
            base.PutValues(model);
            model.CurrentPrice=this.CurrentPrice;
            model.Date=this.Date;
            model.StockCount=this.Count;
            model.Commission=this.Commission;
            model.HeldAt=this.HeldAtAccount ;
            model.StockName=this.Name;
            model.Notes=this.Notes;
            model.PurchasePrice=this.PurchasePrice;
            model.StockSymbol=this.Symbol;
            return model;
        }

        protected void PromptHeldAt()
        {
            
            var stock_vm = new Presentation.ViewModel.StockAccountListViewModel();
            var r = stock_vm.ShowDialog();
            if (r.HasValue && r.Value)
            {
                this.HeldAtAccount=stock_vm.Selected;
            }
        }


        bool? _canPromptHeldAt=null;
        public bool CanPromptHeldAt {
            get { return (_canPromptHeldAt.HasValue)?_canPromptHeldAt.Value:HeldAtAccount==null; }
            set { _canPromptHeldAt = value; }
        }

        public ICommand PromptHeldAtCommand { get; private set; }

        public StockInvestmentViewModel()
        {
            _canPromptHeldAt = null;
            PromptHeldAtCommand = new DelegateCommand(o => this.PromptHeldAt(), o => this.CanPromptHeldAt);
        }
    }
}
